<?php
namespace App\Utilitys;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\AddSmsSignRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class AliSms
{
    //阿里云短信

    protected $accessKeyId = 'LTAI5tRX2JiHHvSc2LQZXamv';
    protected $accessKeySecret = 'zIgBVH2tEauJrzC1cn2Dw6Ffsc9ep8';
    protected $signName = '病友圈';
    protected $tmpCode = 'SMS_307675418';

    //发送
    public function send($phone, $code)
    {
        $tmpCode = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
        $config = new Config([
            "accessKeyId" => $this->accessKeyId,
            "accessKeySecret" => $this->accessKeySecret,
        ]);
        $config->endpoint = "dysmsapi.aliyuncs.com";
        $client = new Dysmsapi($config);
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $phone,  //发送的手机号码
            "signName" => $this->signName,  //签名
            "templateCode" => $this->tmpCode,  //模板CODE
            "templateParam" => $tmpCode,  //这个根据你的模板信息来
        ]);
        $result = $client->sendSms($sendSmsRequest);
        //ddd($result);
        if ($result->body->message == 'OK' && $result->body->code == 'OK') {
            return ['code' => 0,'msg'=>'success'];
        }
        if ($result->body->code == 'isv.MOBILE_NUMBER_ILLEGAL') {
            return ['code' => -2, 'msg' => '手机号码格式不正确'];
        }
        //return ['code' => -2, 'msg' => '短信发送失败，网络繁忙'];
        return ['code' => -2, 'msg' => $result];
    }
}